package com.zqs.socket.listening;

import com.zqs.socket.utils.Command;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
 * @Program: Socket
 * @Description:
 * @Author: zqs
 * @Create： 2021-08-23-17:04
 */


public class Server {
    protected static final Log log = LogFactory.getLog(Server.class);
    /**
     * Socket服务端
     */
    public static void socketServer() {
        try {
            ServerSocket serverSocket = new ServerSocket(3722);
            System.out.println("服务端已启动，监听客户端连接..");
            while (true) {
                Socket socket = serverSocket.accept();//侦听并接受到此套接字的连接,返回一个Socket对象
                // 处理这次连接
                new HandlerThread(socket);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static class HandlerThread implements Runnable {
        private Socket socket;

        public HandlerThread(Socket socket) {
            this.socket = socket;
            new Thread(this).start();
        }
        public void run() {
            try {
                //根据输入输出流和客户端连接
                InputStream inputStream = socket.getInputStream();//得到一个输入流，接收客户端传递的信息
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);//提高效率，将自己字节流转为字符流
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);//加入缓冲区
                String temp = null;
                String info = "";
                while ((temp = bufferedReader.readLine()) != null) {
                    info += temp;
                    log.info("已接收到客户端消息");
//                System.out.println("服务端接收到客户端信息："+info+",当前客户端ip为："+socket.getInetAddress().getHostAddress());
                    if (info.equals("IP has changed")) {
                        for (int i = 0; i < 4; i++) {
                            String commandStr = "nginx -s reload";
                            Command.shell(commandStr);
                            log.info("命令执行完毕");
                            Thread.sleep(180000);
                        }
                    }else {
                        log.info("消息不匹配，未执行");
                    }
                }
//                OutputStream outputStream=socket.getOutputStream();//获取一个输出流，向服务端发送信息
//                PrintWriter printWriter=new PrintWriter(outputStream);//将输出流包装成打印流
//                printWriter.print("服务端已接收到信息,并执行了命令");
//                printWriter.flush();
//                socket.shutdownOutput();//关闭输出流
                //关闭相对应的资源
//                printWriter.close();
//                outputStream.close();
                bufferedReader.close();
                inputStream.close();
            } catch (Exception e) {
                log.warn("服务器 run 异常: " + e.getMessage());
            } finally {
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (Exception e) {
                        socket = null;
                        log.warn("服务端 finally 异常:" + e.getMessage());
                    }
                }
            }
        }
    }
}
